package tcu.yuyp;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import tcu.yuyp.api.InvitationsServlet;
import tcu.yuyp.api.LoginFilter;
import tcu.yuyp.api.LoginServlet;
import tcu.yuyp.api.MembersServlet;
import tcu.yuyp.pojo.Invitation;
import tcu.yuyp.pojo.User;
import tcu.yuyp.repository.InvitationsRepository;
import tcu.yuyp.service.impl.InvitationsServiceImpl;
import tcu.yuyp.service.impl.LoginService;
import tcu.yuyp.service.impl.MembersService;

@Configuration
@ComponentScan
//基于Java源代码的依赖注入的配置
public class AppConfiguration {
    //将Javabean类进行集中的管理

    @Bean
    public User user(){
        return new User();
    }

    @Bean
    public Invitation invitation(){
        return new Invitation();
    }

    @Bean
    public MembersService membersService(){
        return new MembersService();
    }

    @Bean
    public LoginService loginService(){
        return new LoginService();
    }

    @Bean
    public InvitationsServiceImpl invitationsService(InvitationsRepository invitationsRepository){
        return new InvitationsServiceImpl(invitationsRepository);
    }



    /*配置servlet类*/
    @Bean
    public ServletRegistrationBean<LoginServlet> loginServlet(LoginService loginService){
        //注入LoginService的对象
        return new ServletRegistrationBean<>(new LoginServlet(loginService),"/travel/login");
    }

    @Bean
    public ServletRegistrationBean<InvitationsServlet> invitationsServlet(InvitationsServiceImpl invitationsServiceImpl){
        //注入InvitationsService的对象
        return new ServletRegistrationBean<>(new InvitationsServlet(invitationsServiceImpl),"/travel/invitations/*");
    }

    @Bean
    public ServletRegistrationBean<MembersServlet> membersServlet(MembersService membersService){
        return new ServletRegistrationBean<>(new MembersServlet(membersService),"/members");
    }

    //声明filter类
    @Bean
    public FilterRegistrationBean<LoginFilter> loginFilter() {
        FilterRegistrationBean<LoginFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new LoginFilter());
        registrationBean.addUrlPatterns("/travel/*");
        return registrationBean;
    }


}
